package com.cogent.dynamicform.form.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.cogent.business.helper.DynamicFormHelper;
import com.cogent.common.util.MapUtil;
import com.cogent.dao.BussinessQueryHelper;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.domain.FormDomain;
import com.cogent.dynamicform.domain.FormQueryDynTableDomain;

public class QueryDynTableFormImpl {	

	public String genQueryDynTableInput(FormDomain formdomain, List formColumnDomainList, HashMap formMap) {
		StringBuffer str = new StringBuffer();
		Iterator it = formColumnDomainList.iterator();
		str.append("<tr class=\"tr_head\"><td class=\"td_last\">序号</td>");
		while(it.hasNext()) {
			FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
			if(formColumnDomain.getShowtype() == 2)
				continue;
			if(!(formColumnDomain.getType().equals("HIDDEN") || formColumnDomain.getType().equals("DISABLED")))
				str.append("<td class=\"td_last\">").append(formColumnDomain.getCn()).append("</td>");
		}
		Object obj = formMap.get(DynamicFormHelper.QUERY_DYNTABLE_FORM_KEY);
		formMap.remove(DynamicFormHelper.QUERY_DYNTABLE_FORM_KEY);
		if(obj == null) {
			FormQueryDynTableDomain formQueryDynTableDomain = formdomain.getQueryDynTableDomain();
			String[] temps = formQueryDynTableDomain.getQuerysql().split("\\|");
			obj = BussinessQueryHelper.query(temps[1], temps[0]);
		}
		List curList = (List)obj;
		for(int i=0; i<curList.size(); i++) {
			HashMap curdata = (HashMap)curList.get(i);
			str.append("<tr><td class=\"td_last\" id=\"order\">").append(i+1).append("</td>");
			it = formColumnDomainList.iterator();
			int num = 0;
			while(it.hasNext()) {
				FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
				if(formColumnDomain.getShowtype() == 2)
					continue;
				if(!(formColumnDomain.getType().equals("HIDDEN") || formColumnDomain.getType().equals("DISABLED")))
					str.append("<td class=\"td_last\">");
				if(num == 0)
					str.append("<input type=\"hidden\" name=\"QUERYDYNTABLE[").append(i).append("]~id\" value=\"")
						.append(MapUtil.getStringValue(curdata, "id")).append("\"/>");
				String name = formColumnDomain.getName();
				formColumnDomain.setName("QUERYDYNTABLE[" + i + "]~" + formColumnDomain.getName());
				str.append(DynamicFormHelper.genInputField(formColumnDomain, curdata));
				formColumnDomain.setName(name);
				if(!(formColumnDomain.getType().equals("HIDDEN") || formColumnDomain.getType().equals("DISABLED")))
					str.append("</td>");
				num++;
			}
			str.append("</tr>");
		}
			
		return str.toString();
	}
	
	public String getQueryDynTableView(List formColumnDomainList, List viewVlaueList) {
		StringBuffer str = new StringBuffer();
		Iterator it = formColumnDomainList.iterator();
		str.append("<tr class=\"tr_head\"><td class=\"td_last\">序号</td>");
		while(it.hasNext()) {
			FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
			if(formColumnDomain.getShowtype() == 1)
				continue;
			if(!(formColumnDomain.getType().equals("HIDDEN") || formColumnDomain.getType().equals("DISABLED")))
				str.append("<td class=\"td_last\">").append(formColumnDomain.getCn()).append("</td>");
		}
		str.append("</tr>");
		for(int i=0; i<viewVlaueList.size(); i++) {
			HashMap curdata = (HashMap)viewVlaueList.get(i);
			str.append("<tr><td class=\"td_last\" id=\"order\">").append(i+1).append("</td>");
			it = formColumnDomainList.iterator();
			int num = 0;
			while(it.hasNext()) {
				FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
				if(formColumnDomain.getShowtype() == 1)
					continue;
				if(formColumnDomain.getType().equals("HIDDEN") || formColumnDomain.getType().equals("DISABLED"))
					
					continue;
				str.append("<td class=\"td_last\">");
				str.append(DynamicFormHelper.getShowValue(formColumnDomain, curdata));
				str.append("</td>");
			}
			str.append("</tr>");
		}
		
		return str.toString();
	}
}
